package me.xcmc.org.events;

import me.xcmc.org.api.Owner;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.entity.EntityPickupItemEvent;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.*;

import java.util.Objects;

public class PlayerEvent implements Listener {
    @EventHandler
    public void onBreak(BlockBreakEvent e){
        ItemStack iS = Objects.requireNonNull(e.getPlayer().getEquipment()).getItemInMainHand();
        Player p = e.getPlayer();
        if (Owner.isStarsItem(iS)){
            if (Owner.isBind(iS)){
                Owner.setBind(iS,e.getPlayer());
                Objects.requireNonNull(e.getPlayer().getEquipment()).setItemInMainHand(iS);
            }else {
                if (!Owner.isOwner(iS,e.getPlayer())){
                    p.sendTitle("§4警告！","§c你不是该物品的主人",3,5,3);
                    e.setCancelled(true);
                }
            }
        }
    }
    @EventHandler
    public void onPickUp(EntityPickupItemEvent e){
        Player p = (Player) e.getEntity();
        ItemStack is = e.getItem().getItemStack();
        if (Owner.isStarsItem(is)){
            if (e.getEntity() instanceof  Player){
                if (!Owner.isBind(is)){
                    if (!Owner.isOwner(is,p)){
                        e.setCancelled(true);
                    }
                }
            }
            else {
                e.setCancelled(true);
            }
        }
    }
    @EventHandler
    public void Interact(PlayerInteractEvent e){
        if (e.hasItem()){
            ItemStack is = e.getItem();
            if (Owner.isStarsItem(is)){
                if (!Owner.isOwner(is,e.getPlayer())){
                    if (e.getAction() != Action.LEFT_CLICK_BLOCK){
                        e.setCancelled(true);
                    }
                }
            }
        }
    }
    @EventHandler
    public void Drop(PlayerDropItemEvent e){
        ItemStack is = e.getItemDrop().getItemStack();
        if (Owner.isStarsItem(is)){
            if (Owner.isOwner(is,e.getPlayer())){
                e.getItemDrop().remove();
                e.getPlayer().getInventory().addItem(is);
                e.getPlayer().sendTitle("§4警告！","§e这是一件贵重的物品,无法被丢弃！",20,50,20);
            }
        }
    }


    @EventHandler
    public void onAnvil(InventoryClickEvent e){
        ItemStack is =e.getCurrentItem();
        Player p = (Player) e.getWhoClicked();
        if (Owner.isStarsItem(is)){
            if (Owner.isOwner(is,p)){
                // 铁砧
                if (e.getInventory() instanceof AnvilInventory){
                    if (e.getRawSlot() == 2){
                        e.setCancelled(true);
                    }
                }
                // 附魔台
                if (e.getInventory() instanceof EnchantingInventory){
                    e.setCancelled(true);
                }
                // 熔炉
                if (e.getInventory() instanceof FurnaceInventory){
                    e.setCancelled(true);
                }
                // 发射器
                if (e.getView().getType() == InventoryType.DISPENSER){
                    e.setCancelled(true);
                }
                // 投掷器
                if (e.getView().getType() == InventoryType.DROPPER){
                    e.setCancelled(true);
                }
                // 工作台
                if (e.getView().getType() == InventoryType.WORKBENCH){
                    e.setCancelled(true);
                }
                // 背包内的工作台
                if (e.getView().getType() == InventoryType.CRAFTING){
                    e.setCancelled(true);
                }
                // 酿造台
                if (e.getView().getType() == InventoryType.BREWING){
                    e.setCancelled(true);
                }
                // MERCHANT
                if (e.getView().getType() == InventoryType.MERCHANT){
                    e.setCancelled(true);
                }
                // 锻造台
                if (e.getView().getType() == InventoryType.SMITHING){
                    e.setCancelled(true);
                }
                // 信标
                if (e.getView().getType() == InventoryType.BEACON){
                    e.setCancelled(true);
                }
                // 高炉
                if (e.getView().getType() == InventoryType.BLAST_FURNACE){
                    e.setCancelled(true);
                }
                //讲台
                if (e.getView().getType() == InventoryType.LECTERN){
                    e.setCancelled(true);
                }
                //烟熏炉
                if (e.getView().getType() == InventoryType.SMOKER){
                    e.setCancelled(true);
                }
                //织布机
                if (e.getView().getType() == InventoryType.LOOM){
                    e.setCancelled(true);
                }
                //制图台
                if (e.getView().getType() == InventoryType.CARTOGRAPHY){
                    e.setCancelled(true);
                }
                // 砂轮
                if (e.getView().getType() == InventoryType.GRINDSTONE){
                    e.setCancelled(true);
                }
                //切石机
                if (e.getView().getType() == InventoryType.STONECUTTER){
                    e.setCancelled(true);
                }

            }
        }
    }
}
